package com.think.teaching.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.think.teaching.model.Question;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试题 Mapper 接口
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
public interface QuestionMapper extends BaseMapper<Question> {
	
	
	
	@Select("SELECT question.*,course.course_name FROM question LEFT JOIN course ON course.id=question.course_id WHERE question.id=#{id} AND question.delflag = 0")
	@Results({
		@Result(column = "id",property = "id"),
		@Result(column = "course_id",property = "courseId"),
		@Result(column = "type",property = "type"),
		@Result(column = "content",property = "content"),
		@Result(column = "answer",property = "answer"),
		@Result(column = "analyzing",property = "analyzing"),
		@Result(column = "state",property = "state"),
		@Result(column = "id",property = "selections",one = @One(select = "com.think.teaching.mapper.QuestionMapper.GetQuestionSelections",fetchType = FetchType.EAGER)),

	})
	Map<String, Object> GetQuestionDetails(Integer id);
	
	@Select("SELECT content FROM question_selection WHERE question_id=#{id}")
	List<String> GetQuestionSelections(Integer id);



	@Select("<script>"+
			"SELECT question.*,course_name FROM question,course WHERE " +
			"<if test='withoutBan==true'>question.state=1 AND </if>" +
			"<if test='type!=null'>question.type=#{type} AND</if>"+
			"<if test='courseId gt 0'> course_id=#{courseId} AND</if>"+
			"question.delflag=0 AND course.id=question.course_id"+
			"</script>")
	@Result(column = "id",property = "id")
	@Result(column = "id",property = "selections",one = @One(select = "com" +
			".think.teaching.mapper.QuestionMapper.GetQuestionSelections",fetchType = FetchType.EAGER))
	IPage<Map<String, Object>> ListQuestionWithTagAndType(IPage<Map<String, Object>> page,
											   @Param("type") Integer type,
														  @Param("withoutBan") Boolean withoutBan,
														  @Param("courseId") Integer courseId);

}
